#include "../constant.h"

// 基于数组的栈
typedef struct stack
{
    DataType data[Capacity];
    int top; // 栈顶位置
} * Stack;

#include "stack.h"

Stack InitStack()
{
    Stack s = (stack *)malloc(sizeof(stack));
    s->top = -1;
    return s;
}

DataType Push(Stack &s, DataType x)
{
    if (IsFull(s))
    {
        error("full stack");
    }
    s->data[++s->top] = x;
    if (s->top >= Capacity * 0.8)
    {
        warning("Capacity is used over 80%!\n");
    }
    return x;
}

DataType Pop(Stack &s)
{
    if (IsEmpty(s))
    {
        warning("empty stack");
    }
    return IsEmpty(s) ? 0 : s->data[--s->top];
}

DataType Front(Stack s)
{
    if (IsEmpty(s))
    {
        warning("empty stack");
    }
    return s->data[s->top];
}

int IsEmpty(Stack s)
{
    return s->top <= -1;
}

int IsFull(Stack s)
{
    return s->top >= Capacity;
}

void printf(Stack s)
{
    int p = s->top;
    while (p > -1)
    {
        printf("%d ", s->data[p--]);
    }
    printf("\n⬆(top)\n");
}
